home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / ksharedptr.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  5.1 KB  |  176 lines

  1. /* This file is part of the KDE libraries
  2.    Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License version 2 as published by the Free Software Foundation.
  7.  
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.  
  13.    You should have received a copy of the GNU Library General Public License
  14.    along with this library; see the file COPYING.LIB.  If not, write to
  15.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16.    Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef KSharedPTR_H
  19. #define KSharedPTR_H
  20.  
  21. #include "kdelibs_export.h"
  22.  
  23. /**
  24.  * Reference counting for shared objects.  If you derive your object
  25.  * from this class, then you may use it in conjunction with
  26.  * KSharedPtr to control the lifetime of your object.
  27.  *
  28.  * Specifically, all classes that derive from KShared have an internal
  29.  * counter keeping track of how many other objects have a reference to
  30.  * their object.  If used with KSharedPtr, then your object will
  31.  * not be deleted until all references to the object have been
  32.  * released.
  33.  *
  34.  * You should probably not ever use any of the methods in this class
  35.  * directly -- let the KSharedPtr take care of that.  Just derive
  36.  * your class from KShared and forget about it.
  37.  *
  38.  * @author Waldo Bastian <bastian@kde.org>
  39.  */
  40. class KDECORE_EXPORT KShared {
  41. public:
  42.    /**
  43.     * Standard constructor.  This will initialize the reference count
  44.     * on this object to 0.
  45.     */
  46.    KShared() : count(0) { }
  47.  
  48.    /**
  49.     * Copy constructor.  This will @em not actually copy the objects
  50.     * but it will initialize the reference count on this object to 0.
  51.     */
  52.    KShared( const KShared & ) : count(0) { }
  53.  
  54.    /**
  55.     * Overloaded assignment operator.
  56.     */
  57.    KShared &operator=(const KShared & ) { return *this; }
  58.  
  59.    /**
  60.     * Increases the reference count by one.
  61.     */
  62.    void _KShared_ref() const { count++; }
  63.  
  64.    /**
  65.     * Releases a reference (decreases the reference count by one).  If
  66.     * the count goes to 0, this object will delete itself.
  67.     */
  68.    void _KShared_unref() const { if (!--count) delete this; }
  69.  
  70.    /**
  71.     * Return the current number of references held.
  72.     *
  73.     * @return Number of references
  74.     */
  75.    int _KShared_count() const { return count; }
  76.  
  77. protected:
  78.    virtual ~KShared() { }
  79. private:
  80.    mutable int count;
  81. };
  82.  
  83. /**
  84.  * Can be used to control the lifetime of an object that has derived
  85.  * KShared. As long a someone holds a KSharedPtr on some KShared
  86.  * object it won't become deleted but is deleted once its reference
  87.  * count is 0.  This struct emulates C++ pointers virtually perfectly.
  88.  * So just use it like a simple C++ pointer.
  89.  *
  90.  * KShared and KSharedPtr are preferred over QShared / QSharedPtr
  91.  * since they are more safe.
  92.  *
  93.  * WARNING: Please note that this class template provides an implicit
  94.  * conversion to T*. Do *not* change this pointer or the pointee (don't
  95.  * call delete on it, for instance) behind KSharedPtr's back.
  96.  *
  97.  * @author Waldo Bastian <bastian@kde.org>
  98.  */
  99. template< class T >
  100. class KSharedPtr
  101. {
  102. public:
  103. /**
  104.  * Creates a null pointer.
  105.  */
  106.   KSharedPtr()
  107.     : ptr(0) { }
  108.   /**
  109.    * Creates a new pointer.
  110.    * @param t the pointer
  111.    */
  112.   KSharedPtr( T* t )
  113.     : ptr(t) { if ( ptr ) ptr->_KShared_ref(); }
  114.  
  115.   /**
  116.    * Copies a pointer.
  117.    * @param p the pointer to copy
  118.    */
  119.   KSharedPtr( const KSharedPtr& p )
  120.     : ptr(p.ptr) { if ( ptr ) ptr->_KShared_ref(); }
  121.  
  122.   /**
  123.    * Unreferences the object that this pointer points to. If it was
  124.    * the last reference, the object will be deleted.
  125.    */
  126.   ~KSharedPtr() { if ( ptr ) ptr->_KShared_unref(); }
  127.  
  128.   KSharedPtr<T>& operator= ( const KSharedPtr<T>& p ) {
  129.     if ( ptr == p.ptr ) return *this;
  130.     if ( ptr ) ptr->_KShared_unref();
  131.     ptr = p.ptr;
  132.     if ( ptr ) ptr->_KShared_ref();
  133.     return *this;
  134.   }
  135.   KSharedPtr<T>& operator= ( T* p ) {
  136.     if ( ptr == p ) return *this;
  137.     if ( ptr ) ptr->_KShared_unref();
  138.     ptr = p;
  139.     if ( ptr ) ptr->_KShared_ref();
  140.     return *this;
  141.   }
  142.   bool operator== ( const KSharedPtr<T>& p ) const { return ( ptr == p.ptr ); }
  143.   bool operator!= ( const KSharedPtr<T>& p ) const { return ( ptr != p.ptr ); }
  144.   bool operator== ( const T* p ) const { return ( ptr == p ); }
  145.   bool operator!= ( const T* p ) const { return ( ptr != p ); }
  146.   bool operator!() const { return ( ptr == 0 ); }
  147.   operator T*() const { return ptr; }
  148.  
  149.   /**
  150.    * Returns the pointer.
  151.    * @return the pointer
  152.    */
  153.   T* data() { return ptr; }
  154.  
  155.   /**
  156.    * Returns the pointer.
  157.    * @return the pointer
  158.    */
  159.   const T* data() const { return ptr; }
  160.  
  161.   const T& operator*() const { return *ptr; }
  162.   T& operator*() { return *ptr; }
  163.   const T* operator->() const { return ptr; }
  164.   T* operator->() { return ptr; }
  165.  
  166.   /**
  167.    * Returns the number of references.
  168.    * @return the number of references
  169.    */
  170.   int count() const { return ptr->_KShared_count(); } // for debugging purposes
  171. private:
  172.   T* ptr;
  173. };
  174.  
  175. #endif
  176.